﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiMiModel;
using XiMiModelDto;

namespace XiMiDal
{
    public class PhoneDal
    {
        private readonly AppDbContext _appDbContext;
        public PhoneDal(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }
        //商品显示
        public List<ShowModel> Show(int pageindex,int pagesize,string gname,out int totalcount)
        {
            var query = from a in _appDbContext.Goodss
                        join e in _appDbContext.Addresss on a.AId equals e.AId
                        join b in _appDbContext.Citys on e.PId equals b.PId
                        join c in _appDbContext.Citys on e.CId equals c.PId
                        join d in _appDbContext.Citys on e.TId equals d.PId
                        select new ShowModel
                        {
                            GId = a.GId,
                            GName = a.GName,
                            GBan = a.GBan,
                            GColor = a.GColor,
                            GImg = "http://localhost:20338/StaticFiles/" + a.GImg,
                            GNum = a.GNum,
                            GPrice = a.GPrice,
                            GRemark = a.GRemark,
                            AId=e.AId,
                            PId = b.PId,
                            PName = b.PName,
                            CId = c.PId,
                            CName = c.PName,
                            TId = d.PId,
                            TName = d.PName,
                            AddressName=e.AddressName,
                            AName=e.AName,
                            Phone=e.Phone,
                            Remark=e.Remark,
                            You=e.You
                        };
            if(gname!=null)
            {
                query = query.Where(m => m.GName.Contains(gname));
            }
            totalcount = query.Count();
            return query.OrderBy(m => m.GId).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
        }
        //登录
        public UserModel Login(string uname,string password)
        {
            return _appDbContext.Users.Where(m => m.UName == uname && m.Password == password).FirstOrDefault();
        }
        //注册
        public int Register(UserModel userModel)
        {
            _appDbContext.Users.Add(userModel);
            return _appDbContext.SaveChanges();
        }
        public GoodsModel Fan(int id)
        {
           return _appDbContext.Goodss.FirstOrDefault(m => m.GId == id);
        }
       public int Add(ShopModel shopModel)
       {
           _appDbContext.Shops.Add(shopModel);
           //var result= _appDbContext.Shops.FirstOrDefault(m => m.GId == shopModel.GId);
           //if (result!=null)
           // {
           //     shopModel.SNum += shopModel.SNum;
           // }
           return _appDbContext.SaveChanges();
       }
    }
}
